package cn.ctodb.divination.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.divination.service.HzwxService;
import cn.ctodb.divination.web.rest.util.HeaderUtil;
import cn.ctodb.divination.service.dto.HzwxDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Hzwx.
 */
@RestController
@RequestMapping("/api")
public class HzwxResource {

    private final Logger log = LoggerFactory.getLogger(HzwxResource.class);

    private static final String ENTITY_NAME = "hzwx";
        
    private final HzwxService hzwxService;

    public HzwxResource(HzwxService hzwxService) {
        this.hzwxService = hzwxService;
    }

    /**
     * POST  /hzwxes : Create a new hzwx.
     *
     * @param hzwxDTO the hzwxDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new hzwxDTO, or with status 400 (Bad Request) if the hzwx has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/hzwxes")
    @Timed
    public ResponseEntity<HzwxDTO> createHzwx(@RequestBody HzwxDTO hzwxDTO) throws URISyntaxException {
        log.debug("REST request to save Hzwx : {}", hzwxDTO);
        if (hzwxDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new hzwx cannot already have an ID")).body(null);
        }
        HzwxDTO result = hzwxService.save(hzwxDTO);
        return ResponseEntity.created(new URI("/api/hzwxes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /hzwxes : Updates an existing hzwx.
     *
     * @param hzwxDTO the hzwxDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated hzwxDTO,
     * or with status 400 (Bad Request) if the hzwxDTO is not valid,
     * or with status 500 (Internal Server Error) if the hzwxDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/hzwxes")
    @Timed
    public ResponseEntity<HzwxDTO> updateHzwx(@RequestBody HzwxDTO hzwxDTO) throws URISyntaxException {
        log.debug("REST request to update Hzwx : {}", hzwxDTO);
        if (hzwxDTO.getId() == null) {
            return createHzwx(hzwxDTO);
        }
        HzwxDTO result = hzwxService.save(hzwxDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, hzwxDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /hzwxes : get all the hzwxes.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of hzwxes in body
     */
    @GetMapping("/hzwxes")
    @Timed
    public List<HzwxDTO> getAllHzwxes() {
        log.debug("REST request to get all Hzwxes");
        return hzwxService.findAll();
    }

    /**
     * GET  /hzwxes/:id : get the "id" hzwx.
     *
     * @param id the id of the hzwxDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the hzwxDTO, or with status 404 (Not Found)
     */
    @GetMapping("/hzwxes/{id}")
    @Timed
    public ResponseEntity<HzwxDTO> getHzwx(@PathVariable Long id) {
        log.debug("REST request to get Hzwx : {}", id);
        HzwxDTO hzwxDTO = hzwxService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(hzwxDTO));
    }

    /**
     * DELETE  /hzwxes/:id : delete the "id" hzwx.
     *
     * @param id the id of the hzwxDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/hzwxes/{id}")
    @Timed
    public ResponseEntity<Void> deleteHzwx(@PathVariable Long id) {
        log.debug("REST request to delete Hzwx : {}", id);
        hzwxService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/hzwxes?query=:query : search for the hzwx corresponding
     * to the query.
     *
     * @param query the query of the hzwx search 
     * @return the result of the search
     */
    @GetMapping("/_search/hzwxes")
    @Timed
    public List<HzwxDTO> searchHzwxes(@RequestParam String query) {
        log.debug("REST request to search Hzwxes for query {}", query);
        return hzwxService.search(query);
    }


}
